package com.hnkywl.sts.service.zjgl;

import com.hnkywl.sts.dao.cwgl.KhzhsfmxDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.sfgl.FkshDao;
import com.hnkywl.sts.dao.zjgl.PjglDao;
import com.hnkywl.sts.dao.zjgl.XyzDao;
import com.hnkywl.sts.dao.zjgl.XyzfyDao;
import com.hnkywl.sts.entity.cwgl.Khzhsfmx;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.zjgl.Pj;
import com.hnkywl.sts.entity.zjgl.Xyz;
import com.hnkywl.sts.entity.zjgl.Xyzfy;
import com.hnkywl.sts.service.cwgl.KhzhsfmxService;
import com.hnkywl.sts.service.organ.KhhService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.service.sfgl
 * 类描述:   付款审核服务类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Service
public class XyzService {
    private XyzDao xyzDao;
    private GsjgDao gsjgDao;
    private KhzhsfmxService khzhsfmxService;
    private KhzhsfmxDao khzhsfmxDao;
    private YgService ygService;
    private KhhService khhService;
    private XyzfyDao xyzfyDao;
    private FkshDao fkshDao;


    @Transactional
    public String findAllXyz(String xyzhm,Long sfkId,Integer xyzsflxType,Integer shztType,Long gsId,String gcMc,Long khhId,String khhMc,Integer zt,int page,int  rows) {
        Long nbgsIds = null;
        Long khhIds = null;
        if(gsId !=null && gcMc.equals(gsjgDao.get(gsId).getJc())){
           nbgsIds = gsId;
        }
        if(khhId!=null && khhMc.equals(khhService.findKhhById(khhId).getQc())){
           khhIds = khhId;
        }
        Page<Xyz> xyzPage = this.xyzDao.findXyzList(xyzhm,sfkId,xyzsflxType, shztType, gcMc,nbgsIds,khhMc,khhIds,zt,page, rows);
        List<Xyz> xyzList = xyzPage.getResult() == null ? new ArrayList<Xyz>() : xyzPage.getResult();
        String xyzJson;
        if(xyzsflxType==0){
            xyzJson = BaseUtils.fetchCollectionToJson(xyzList, "id", "nbgs.jc",  "xyzhm", "shzt",
                    "ysczyh","dqrq","dqje" ,"czr","bzj","skr" ,"kzf","sfdrlx","sfwffx","lxe","jkrq","bz","status");
        }else if(xyzsflxType==1){
            xyzJson = BaseUtils.fetchCollectionToJson(xyzList, "id", "nbgs.jc",  "xyzhm",
                    "yfczyh.qc","dqrq","dqje" ,"bzj","jzdw.jc","kzf","sfdrlx","sfwffx","lxe","jkrq","bz","status");
        } else{
            xyzJson = null;
        }

        return "{\"total\":" + xyzPage.getTotalCount() + ",\"rows\":[" + xyzJson + "]}";
    }

    @Transactional
    public String findxyzListByIds(String xyzIds,Integer xyzsflxType,int page,int  rows) {
        Page<Xyz> pjglPage = this.xyzDao.findxyzListByIds(xyzIds,xyzsflxType,page, rows);
        List<Xyz> pjglList = pjglPage.getResult() == null ? new ArrayList<Xyz>() : pjglPage.getResult();
        String pjglJson;
        if(xyzsflxType==0){
            pjglJson = BaseUtils.fetchCollectionToJson(pjglList, "id", "nbgs.jc", "yll", "xyzhm", "shzt",
                    "ysczyh","dqrq","dqje" ,"czr","bzj","skr" ,"sfdrlx","kzf","jzdw.jc","sfwffx","lxe","jkrq","bz","status");
        }else if(xyzsflxType==1){
            pjglJson = BaseUtils.fetchCollectionToJson(pjglList, "id", "nbgs.jc", "yll", "xyzhm",
                    "yfczyh.qc","dqrq","dqje" ,"bzj","jzdw.jc","sfdrlx","kzf","sfwffx","lxe","jkrq","bz","status");
        } else{
            pjglJson = null;
        }

        return "{\"total\":" + pjglPage.getTotalCount() + ",\"rows\":[" + pjglJson + "]}";
    }




    @Transactional
    public void saveXyz(Xyz xyz) {
        xyz.setShzt(0);
        xyz.setDateCreated(new Date());
        xyz.setCreateUser(YgService.getCurrentYg());
        xyz.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xyz.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xyz.setLastUpdated(xyz.getDateCreated());
        xyz.setStatus(1);
        xyz.setSfdrlx("wdr");
        this.xyzDao.save(xyz);
        Xyzfy xyzfy = new Xyzfy();
        xyzfy.setXyz(xyz);
        xyzfy.setDateCreated(new Date());
        xyzfy.setCreateUser(YgService.getCurrentYg());
        xyzfy.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xyzfy.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xyzfy.setLastUpdated(xyz.getDateCreated());
        xyzfy.setBzj(xyz.getBzj());
        xyzfy.setKzf(xyz.getKzf());
        this.xyzfyDao.save(xyzfy);
    }

    /**
     * 更新应付账款票据管理
     * @param xyz 票据管理实体
     * @return
     */
    @Transactional
    public void updateXyz(Xyz xyz) {
        Xyz clone = this.findXyzDetail(xyz.getId());
        Integer stat = clone.getStatus();
        clone = (Xyz) ReflectionUtils.getInstance(clone, xyz);
        clone.setLastUpdated(new Date());
        clone.setStatus(stat);
        this.xyzDao.save(clone);
        if(xyz.getXyzsflx()==1){
            Xyzfy xyzfy = this.xyzfyDao.findXyzfyByXyzId(xyz.getId());
            xyzfy.setBzj(clone.getBzj());
            xyzfy.setKzf(clone.getKzf());
            this.xyzfyDao.save(xyzfy);
        }
    }

    @Transactional
    public void deleteXyz(Long id) {
        Xyz xyz = this.findXyzDetail(id);
        if(xyz.getXyzsflx()==0){
            throw new RuntimeException("应收信用证请删除关联收款申请");
        }
        xyz.setLastUpdated(new Date());
        xyz.setDeleted(1);
        this.xyzDao.save(xyz);
        //如果信用证首付类型是应收信用证 那么就要将关联的收付款入账去掉
        if(xyz.getXyzsflx()==1){
            Xyzfy xyzfy =  xyzfyDao.findXyzfyByXyzId(xyz.getId());
            if(xyzfy.getInsertFlag()==3){
                throw new RuntimeException("该信用证费用已经导入财务系统，请先撤销");
            }
            xyzfy.setLastUpdated(new Date());
            xyzfy.setDeleted(1);
            this.xyzfyDao.save(xyzfy);
        }
    }

    @Transactional
    public void saveOldXyz(Xyz xyz) {
        this.xyzDao.save(xyz);
    }
    /**
     * 信用证明细查找
     * @param id
     * @return
     */
    @Transactional
    public Xyz findXyzDetail(Long id) {
        return   this.xyzDao.get(id);
    }


    @Transactional
    public String findXyzWsy(Integer xyzsflxType, int page,int  rows) {
        Page<Xyz> xyzlPage = this.xyzDao.findwsyXyzList(xyzsflxType, page, rows);
        List<Xyz> xyzList = xyzlPage.getResult() == null ? new ArrayList<Xyz>() : xyzlPage.getResult();
        String xyzJson;
       if(xyzsflxType==1){
           xyzJson = BaseUtils.fetchCollectionToJson(xyzList, "id", "nbgs.jc",  "xyzhm",
                    "yfczyh.qc","dqrq","bzj","dqje" ,"jzdw.jc","yll","lxe","jkrq","bz","status");
        } else{
           xyzJson = null;
        }
        return "{\"total\":" + xyzlPage.getTotalCount() + ",\"rows\":[" + xyzJson + "]}";
    }

    @Transactional
    public void hkXyz(String xyzIds,Long sqrId) {
        String[] xids = xyzIds.split(",");
        for(String id:xids){
            Xyz xyz = this.findXyzDetail(Long.parseLong(id));
            xyz.setHkrq(new Date());
            xyz.setLastUpdated(new Date());
            xyz.setStatus(2);
            //到期金额银行存款表操作
            Khzhsfmx khzhsfmx = new Khzhsfmx();
            khzhsfmx.setNbgs(xyz.getNbgs());
            khzhsfmx.setType(2);
            khzhsfmx.setRzyh(khhService.findKhhById(xyz.getYfczyh().getId()));//拿到页面选择的开户行 只有承兑的时候入账银行是票据的银行
            khzhsfmx.setZh(1);
            if(sqrId==null){
                khzhsfmx.setShr(null);
            }else{
                khzhsfmx.setShr(ygService.findYgDetail(sqrId));
            }
            khzhsfmx.setJe(Double.parseDouble(xyz.getDqje().toString()));
            khzhsfmx.setXyz(xyz);
            khzhsfmx.setZdr(YgService.getCurrentYg());
            khzhsfmx.setGllx(2);
            khzhsfmx.setCreateUser(YgService.getCurrentYg());
            khzhsfmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
            khzhsfmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            khzhsfmx.setDateCreated(new Date());
            khzhsfmx.setLastUpdated(khzhsfmx.getDateCreated());
            this.khzhsfmxService.saveKhzhsfmx(khzhsfmx);
            if(xyz.getSfwffx()==0){
                //利息银行存款表操作
                khzhsfmx = new Khzhsfmx();
                khzhsfmx.setNbgs(xyz.getNbgs());
                khzhsfmx.setType(2);
                khzhsfmx.setRzyh(khhService.findKhhById(xyz.getYfczyh().getId()));//拿到页面选择的开户行 只有承兑的时候入账银行是票据的银行
                khzhsfmx.setZh(1);
                if(sqrId==null){
                    khzhsfmx.setShr(null);
                }else{
                    khzhsfmx.setShr(ygService.findYgDetail(sqrId));
                }
                khzhsfmx.setJe(xyz.getLxe());
                khzhsfmx.setXyz(xyz);
                khzhsfmx.setZdr(YgService.getCurrentYg());
                khzhsfmx.setGllx(3);
                khzhsfmx.setCreateUser(YgService.getCurrentYg());
                khzhsfmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                khzhsfmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                khzhsfmx.setDateCreated(new Date());
                khzhsfmx.setLastUpdated(khzhsfmx.getDateCreated());
                this.khzhsfmxService.saveKhzhsfmx(khzhsfmx);
            }
        }
    }

    @Transactional
    public void bxXyz(String xyzIds,Long sqrId,Long yhmcId) {
        String[] xids = xyzIds.split(",");
        for(String id:xids){
            Xyz xyz = this.findXyzDetail(Long.parseLong(id));
            xyz.setBxrq(new Date());
            xyz.setLastUpdated(new Date());
            xyz.setStatus(3);
            //到期金额银行存款表操作
            Khzhsfmx khzhsfmx = new Khzhsfmx();
            khzhsfmx.setNbgs(xyz.getNbgs());
            khzhsfmx.setType(1);
            //todo
            khzhsfmx.setRzyh(khhService.findKhhById(yhmcId));//拿到页面选择的开户行 只有还款的时候入账银行是票据的银行
            khzhsfmx.setZh(1);
            if(sqrId==null){
                khzhsfmx.setShr(null);
            }else{
                khzhsfmx.setShr(ygService.findYgDetail(sqrId));
            }
            khzhsfmx.setJe(Double.parseDouble(xyz.getDqje().toString()));
            khzhsfmx.setXyz(xyz);
            khzhsfmx.setZdr(YgService.getCurrentYg());
            khzhsfmx.setGllx(2);
            khzhsfmx.setCreateUser(YgService.getCurrentYg());
            khzhsfmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
            khzhsfmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
            khzhsfmx.setDateCreated(new Date());
            khzhsfmx.setLastUpdated(khzhsfmx.getDateCreated());
            this.khzhsfmxService.saveKhzhsfmx(khzhsfmx);
            if(xyz.getSfwffx()==0){
                //利息银行存款表操作
                khzhsfmx = new Khzhsfmx();
                khzhsfmx.setNbgs(xyz.getNbgs());
                khzhsfmx.setType(2);
                //todo
                khzhsfmx.setRzyh(khhService.findKhhById(yhmcId));//拿到页面选择的开户行 只有还款的时候入账银行是票据的银行
                khzhsfmx.setZh(1);
                if(sqrId==null){
                    khzhsfmx.setShr(null);
                }else{
                    khzhsfmx.setShr(ygService.findYgDetail(sqrId));
                }
                khzhsfmx.setJe(xyz.getLxe());
                khzhsfmx.setXyz(xyz);
                khzhsfmx.setZdr(YgService.getCurrentYg());
                khzhsfmx.setGllx(3);
                khzhsfmx.setCreateUser(YgService.getCurrentYg());
                khzhsfmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                khzhsfmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                khzhsfmx.setDateCreated(new Date());
                khzhsfmx.setLastUpdated(khzhsfmx.getDateCreated());
                this.khzhsfmxService.saveKhzhsfmx(khzhsfmx);
            }
        }
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id, String value, Object field) {
        return this.xyzDao.checkUnique(id, value, field);
    }




    @Autowired
    public void setXyzDao(XyzDao xyzDao) {
        this.xyzDao = xyzDao;
    }
    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }
    @Autowired
    public void setKhzhsfmxService(KhzhsfmxService khzhsfmxService) {
        this.khzhsfmxService = khzhsfmxService;
    }
    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

    @Autowired
    public void setKhhService(KhhService khhService) {
        this.khhService = khhService;
    }
    @Autowired
    public void setXyzfyDao(XyzfyDao xyzfyDao) {
        this.xyzfyDao = xyzfyDao;
    }
    @Autowired
    public void setFkshDao(FkshDao fkshDao) {
        this.fkshDao = fkshDao;
    }
    @Autowired
    public void setKhzhsfmxDao(KhzhsfmxDao khzhsfmxDao) {
        this.khzhsfmxDao = khzhsfmxDao;
    }
}
